
window.verify = {
    username : /^[\w]{4,16}$/,
    passwd : /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,12}$/ ,
    qq : /^[1-9][0-9]{5,11}$/ ,
    email : /^([.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/ ,
    //email : /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/ ,
    tel : /^(13[0-9]|15[012356789]|17[0678]|18[0-9]|14[57])[0-9]{8}$/
};

function newVerify() {
    var parentSelector = "#reg_form_1";
    $(parentSelector+"  img[name=\"login_img\"]").attr('src',  '/seccode/makecode.html?nchash=&t=' + Math.random());
    $(parentSelector+"  [name=\"verify\"]").val('');
}

$(function()
{    //检查用户名是否已存在
    $("#reg_form_1  [name=\"username\"]").blur(
        function(){
            var parentSelector = "#reg_form_1";
            var obj = $("#reg_form_1  [name=\"username\"]");
            var value =   $.trim( obj.val());
            if( !verify[obj.attr("name")].test(value ) )
            {
                // _alert("用户名必须是4-16个字母数字组合");
            }else if( value != "" )
            {
                $.ajax({
                    global: false,
                    url: '/register/userExist.html',
                    type: 'post',
                    dataType: 'json',
                    data: {name:value },
                    success: function (data) {
                        try
                        {
                            if ( session_timeout(data) === false )
                            {
                                return false;
                            }
                        } catch(e){ console.log(e);}
                        if ( !data.Result) {
                            $("#_tips").html((data.Desc?data.Desc:"该用户名已存在"));
                            obj.addClass("fl-reg-error-input");
                            obj.parent().parent().find(".reg-notice-del").show();
                            //建议账号
                            if( $("#_tips  .suggest").length > 0 )
                            {
                                $("#_tips  .suggest").click(function(){
                                    $("#reg_form_1  [name=\"username\"]").val($(this).text());
                                });
                            }
                        }
                        else
                        {
                            $("#_tips").html("用户名须为4-16个字母或数字");
                        }
                    }
                });
            }
        }
    );
});

//验证码绑定事件
$(function(){
    var parentSelector = "#reg_form_1";
    $(parentSelector+" [name=\"verify\"]").click(function()
    {
        if( $(parentSelector+" [name=\"div_top_click\"]").css("display") == "none" )
        {
            return;
        }
        newVerify();
        $(parentSelector+" [name=\"div_top_click\"]").hide();
        try{if(event.preventDefault){event.preventDefault();}else{event.returnValue = false;}}catch(e){}
    }).focus(function(){
        if($(this).val()=="")
        {
            newVerify();
        }
        try{if(event.preventDefault){event.preventDefault();}else{event.returnValue = false;}}catch(e){}
    });
    $(parentSelector+" [name=\"div_top_click\"]").click(function()
    {
        if( $(parentSelector+" [name=\"div_top_click\"]").css("display") == "none" )
        {
            return;
        }
        newVerify();
        $(parentSelector+" [name=\"verify\"]").trigger("focus");
        $(parentSelector+" [name=\"div_top_click\"]").hide();
    });
    $(parentSelector+" [name=\"login_img\"],"+parentSelector+" [name=\"btn_refresh\"]").click(function()
    {
        $(parentSelector+" [name=verify]").focus();
    });
});

//提交 注册
function submitRegistr()
{
    var parentSelector = "#reg_form_1";
    var username = $.trim($(parentSelector+"  [name=\"username\"]").val());
    var passwd = $.trim($(parentSelector+"  [name=\"passwd\"]").val());
    var conpasswd = $.trim($(parentSelector+"  [name=\"conpasswd\"]").val());
    var verify_val = $.trim($(parentSelector+"  [name=\"verify\"]").val());
    if( !(window.verify[$(parentSelector+"  [name=\"username\"]").attr("name")].test( username )) )
    {
        _alert("请输入正确格式的用户名");
        $(parentSelector+" [name=\"username\"]").trigger("focus");
        return false;
    }
    if( !(window.verify[$(parentSelector+"  [name=\"passwd\"]").attr("name")].test( passwd )) )
    {
        _alert("请输入正确格式的密码");
        $(parentSelector+" [name=\"passwd\"]").trigger("focus");
        return;
    }
    if( passwd != conpasswd )
    {
        _alert("两次密码不一致");
        $(parentSelector+" [name=\"conpasswd\"]").trigger("focus");
        return;
    }
    var param = {
        supportcode:php_supportcode,
        formhash:$(parentSelector+"  input[name=\"formhash\"]").val(),
        username:username,
        passwd:passwd,
        conpasswd:conpasswd,
        form_submit:"ok",
        verify:verify_val
    };
    //如果存在qq输入框
    if( $(parentSelector+"  #qq").length != 0 )
    {
        if( $.trim($(parentSelector+"  #qq").val())=="" &&  $(parentSelector+"  #qq").parent().parent().find(" .red").length != 0  )
        {
            _alert("qq号码不可为空");
            $(parentSelector+" #qq").trigger("focus");
            return;
        }
        if( $.trim($(parentSelector+"  #qq").val())!="" && !verify[$(parentSelector+"  #qq").attr("id")].test( $.trim($(parentSelector+"  #qq").val()) ) )
        {
            _alert("qq号格式有误");
            $(parentSelector+" #qq").trigger("focus");
            return;
        }
        if( "" != $.trim($(parentSelector+"  #qq").val()) )
        {
            param.qq = $.trim($(parentSelector+"  #qq").val());
        }
    }
    //如果存在email输入框
    if( $(parentSelector+"  #email").length != 0 )
    {
        if( $.trim($(parentSelector+"  #email").val())=="" &&  $(parentSelector+"  #email").parent().parent().find(" .red").length != 0  )
        {
            _alert("email不可为空");
            $(parentSelector+" #email").trigger("focus");
            return;
        }
        if( $.trim($(parentSelector+"  #email").val())!="" && !verify[$(parentSelector+"  #email").attr("id")].test( $.trim($(parentSelector+"  #email").val()) ) )
        {
            _alert("email格式有误");
            $(parentSelector+" #email").trigger("focus");
            return;
        }
        if( "" != $.trim($(parentSelector+"  #email").val()) )
        {
            param.email = $.trim($(parentSelector+"  #email").val());
        }
    }
    //如果存在手机输入框
    if( $(parentSelector+"  #tel").length != 0 )
    {
        if( $.trim($(parentSelector+"  #tel").val())=="" &&  $(parentSelector+"  #tel").parent().parent().find(" .red").length != 0  )
        {
            _alert("手机号码不可为空");
            return;
        }
        if( $.trim($(parentSelector+"  #tel").val())!="" && !verify[$(parentSelector+"  #tel").attr("id")].test( $.trim($(parentSelector+"  #tel").val()) ) )
        {
            _alert("手机号码格式有误");
            return;
        }
        if( "" != $.trim($(parentSelector+"  #tel").val()) )
        {
            param.tel = $.trim($(parentSelector+"  #tel").val());
        }
    }
    if( verify_val == "" || verify_val.length != 4 )
    {
        _alert("请输入正确的验证码");
        $(parentSelector+" [name=\"verify\"]").click();
        return;
    }
    if( !$(parentSelector+" [name=\"reg_checkbox\"]").get(0).checked )
    {
        _alert("请同意服务协议");
        return;
    }
    try{
        if( typeof(window.ajax_url_reg) != "string" )
        {
            window.ajax_url_reg = "/register/index.html";
        }
    }catch(e){
        window.ajax_url_reg = "/register/index.html";
    }
    $.ajax({
        url: window.ajax_url_reg,
        type: 'post',
        dataType: 'json',
        data:param,
        success: function (data) {
            regAjaxSuccess(data);//由于每个页面的逻辑有区别。所以regAjaxSuccess方法定义在每个php页面里面，并没有定义在当前的js文件中
        },
        error: function (data) {
            var parentSelector = "#reg_form_1";
            $(parentSelector+'  #btn-sub').removeClass('submiting').addClass('submit').attr('disabled', false);
            newVerify();
        }
    });
}

$(function(){
    // 注册输入框的回车事件
    (function(){
        var parentSelector = "#reg_form_1";
        $(parentSelector).get(0).onkeydown = function (event) {
            var e = event || window.event || arguments.callee.caller.arguments[0];
            if( e.keyCode == 9 )
            {
                try{if(e.preventDefault){e.preventDefault();}else{e.returnValue = false;}}catch(e){}
            }
            if (e && (e.keyCode == 13 || e.keyCode == 9 ) ) { // enter 键
                if ($.trim($(parentSelector+" [name=\"username\"]").val()) == '')
                {
                    $(parentSelector+" [name=\"username\"]").trigger("focus");
                    return;
                }
                if ($.trim($(parentSelector+" [name=\"passwd\"]").val()) == '')
                {
                    $(parentSelector+" [name=\"passwd\"]").trigger("focus");
                    return;
                }
                if ($.trim($(parentSelector+" [name=\"conpasswd\"]").val()) == '')
                {
                    $(parentSelector+" [name=\"conpasswd\"]").trigger("focus");
                    return;
                }                
                 //如果存在qq输入框
			    if( $(parentSelector+"  #qq").length != 0 && $.trim($(parentSelector+" #qq").val()) == '' )
			    {			
			    	$(parentSelector+" #qq").trigger("focus");
			    	if( $(parentSelector+"  #qq").parent().parent().find(" .red").length != 0  )
			    	{//如果是必填
			    	 	return;
			    	}
			    	else
			    	{//如果是非必填项
			    	 	 var dom = e.target || e.srcElement ;
			    	 	if( dom != $(parentSelector+"  #qq").get(0) && ( dom != $(parentSelector+"  #email").get(0) && dom != $(parentSelector+"  #tel").get(0) && dom != $(parentSelector+"  #verify").get(0) ) )
			    	 	{//如果鼠标不在当前的输入框内（前一个输入框）
			    	 	   return;
			    	 	}			    	 	
			    	}
			    	 //如果鼠标在当前的输入框内(则什么都不执行，不用return，就保证继续下面的逻辑)
			    }		                
                //如果存在email输入框
			    if( $(parentSelector+"  #email").length != 0 && $.trim($(parentSelector+" #email").val()) == '' )
			    {			
			    	$(parentSelector+" #email").trigger("focus");
			    	if( $(parentSelector+"  #email").parent().parent().find(" .red").length != 0  )
			    	{//如果是必填
			    	 	return;
			    	}
			    	else
			    	{//如果是非必填项
			    	 	var dom = e.target || e.srcElement ;
			    	 	if( dom != $(parentSelector+"  #email").get(0)  && (dom != $(parentSelector+"  #tel").get(0) && dom != $(parentSelector+"  #verify").get(0) ) )
			    	 	{//如果鼠标不在当前的输入框内（前一个输入框）
			    	 	   return;
			    	 	}			    	 	
			    	}
			    	 //如果鼠标在当前的输入框内(则什么都不执行，不用return，就保证继续下面的逻辑)
			    }	
                //如果存在tel输入框
			    if( $(parentSelector+"  #tel").length != 0 && $.trim($(parentSelector+" #tel").val()) == '' )
			    {			
			    	$(parentSelector+" #tel").trigger("focus");
			    	if( $(parentSelector+"  #tel").parent().parent().find(" .red").length != 0  )
			    	{//如果是必填
			    	 	return;
			    	}
			    	else
			    	{//如果是非必填项
			    	 	var dom = e.target || e.srcElement ;
			    	 	if( dom != $(parentSelector+"  #tel").get(0) && dom != $(parentSelector+"  #verify").get(0) )
			    	 	{//如果鼠标不在当前的输入框内（前一个输入框）
			    	 	   return;
			    	 	}			    	 	
			    	}//如果鼠标在当前的输入框内(则什么都不执行，不用return，就保证继续下面的逻辑)
			    }
			    
                if ( $(parentSelector+" [name=div_top_click]").length > 0 && ( $(parentSelector+" [name=div_top_click]").css("display")=="block"||$(parentSelector+" [name=div_top_click]").css("display")!="none"))
                {
                    $(parentSelector+" [name=\"div_top_click\"]").trigger("click");
                    return;
                }
                if ($.trim($(parentSelector+" [name=\"verify\"]").val()) == '')
                {
                    $(parentSelector+" [name=\"verify\"]").trigger("focus");
                    return;
                }
                //如果存在qq输入框，且是必填项
                if( $(parentSelector+" #qq").length != 0 &&  $(parentSelector+" #qq").parent().parent().find(" .red").length != 0)
                {
                    if ($.trim($(parentSelector+" #qq").val()) == '') {
                        $(parentSelector+" #qq").trigger("focus");
                        return;
                    }
                }
                //如果存在email输入框，且是必填项
                if( $(parentSelector+" #email").length != 0 &&  $(parentSelector+" #email").parent().parent().find(" .red").length != 0  )
                {
                    if ($.trim($(parentSelector+" #email").val()) == '') {
                        $(parentSelector+" #email").trigger("focus");
                        return;
                    }
                }
                //如果存在手机输入框，且是必填项
                if( $(parentSelector+" #tel").length != 0 &&  $(parentSelector+" #tel").parent().parent().find(" .red").length != 0  )
                {
                    if ($.trim($(parentSelector+" #tel").val()) == '') {
                        $(parentSelector+" #tel").trigger("focus");
                        return;
                    }
                }
                //验证码
                if ($.trim($(parentSelector+" [name=\"verify\"]").val()) == '') {
                    $(parentSelector+" [name=\"verify\"]").trigger("focus");
                    return;
                }
                if( !$(" [name=\"reg_checkbox\"]").get(0).checked )
                {
                    _alert("请同意服务协议");
                    return;
                }
                submitRegistr();
            }
        };
    })();
});